package com.cs.beneficios.web.action;
import com.cs.beneficios.business.model.CondicionConsultaVO;
import com.cs.beneficios.business.model.RepAtributoVO;
import com.cs.beneficios.common.ConnectionFactory;

import java.io.IOException;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TotalSelectFilterRGFAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    
    List lFilters = new LinkedList();
    List lShowFilters = new LinkedList();
    
    boolean elim = false;
    String strValue1 = null;
    String strValue2 = null;
    String strFilter = null;
    String strCodeField = null;
    String strNomField = null;
    String strOrigin = null;
    
    Object objAccion = (Object)request.getParameter("accion");
    Object objAccion2 = (Object)request.getParameter("accion2");
    
    session.removeAttribute("ListReportGrupoFam");
    if(objAccion == null && objAccion2 == null)
    {
      session.removeAttribute("ListFiltrado");
      session.removeAttribute("CodeZona");
      session.removeAttribute("CodeComuna");
      session.removeAttribute("CodeBarrio");
      session.removeAttribute("CodePrograma");
      session.removeAttribute("CodeSubPrograma");
    }
    
    String strCodeZona = (String)request.getParameter("codeZona");
    String strCodeComuna = (String)request.getParameter("codeComuna");
    String strCodeBarrio = (String)request.getParameter("codeBarrio");
    String strCodePrograma = (String)request.getParameter("codePrograma");
    String strCodeSubPrograma = (String)request.getParameter("codeSubPrograma");
    String strDayIni = (String)request.getParameter("dia1");
    String strMonthIni = (String)request.getParameter("mes1");
    String strYearIni = (String)request.getParameter("ano1");
    String strDayFin = (String)request.getParameter("dia2");
    String strMonthFin = (String)request.getParameter("mes2");
    String strYearFin = (String)request.getParameter("ano2");
    
    RepAtributoVO objrepatriVO = null;
    CondicionConsultaVO objCondConsVO = null;
    
    String ArrAtri[] = {"NAME_GRUPOFAM","NAME1_BENE","NAME2_BENE",
                        "APE1_BENE","APE2_BENE","NUMERO_DOCUMENTO","NUMERO_BENEFICIOS"};
    String ArrAtri2[] = {"NOMBRE GRUPO FAMILIAR","PRIMER NOMBRE JEFE DE HOGAR","SEGUNDO NOMBRE JEFE DE HOGAR",
                         "PRIMER APELLIDO JEFE DE HOGAR","SEGUNDO APELLIDO JEFE DE HOGAR","No DOCUMENTO JEFE DE HOGAR",
                         "NUMERO DE BENEFICIOS"};
    
    for(int j=0;j<ArrAtri2.length;j++)
    {
      objrepatriVO = new RepAtributoVO();
      objrepatriVO.setNombre(ArrAtri[j]);
      objrepatriVO.setEtiqueta(ArrAtri2[j]);
      if(j<=5)
        objrepatriVO.setOrigen("A");
      else
        objrepatriVO.setOrigen("N");
      
      lShowFilters.add(objrepatriVO);
    }
    
    if(objAccion != null)
    {
      if(objAccion.equals("Find"))
      {
        strCodeField = request.getParameter("CodeField"); 
        strFilter = request.getParameter("CodeFilter");
        strValue1 = request.getParameter("CodeValue1");
        strValue2 = request.getParameter("CodeValue2");
        Object objFil = (Object)session.getAttribute("ListFiltrado");
        if(objFil!=null)
        {
          lFilters = (List)session.getAttribute("ListFiltrado");
        }
        
        if(strCodeField.equals("NAME_GRUPOFAM"))
        {
          strNomField = "NOMBRE GRUPO FAMILIAR";
          strValue1 = strValue1.toUpperCase();
          strOrigin = "A";
        }else if(strCodeField.equals("NAME1_BENE"))
        {
          strNomField = "PRIMER NOMBRE JEFE DE HOGAR";
          strOrigin = "A";
        }else if(strCodeField.equals("NAME2_BENE"))
        {
          strNomField = "SEGUNDO NOMBRE JEFE DE HOGAR";
          strOrigin = "A";
        }else if(strCodeField.equals("APE1_BENE"))
        {
          strNomField = "PRIMER APELLIDO JEFE DE HOGAR";
          strOrigin = "A";
        }else if(strCodeField.equals("APE2_BENE"))
        {
          strNomField = "SEGUNDO APELLIDO JEFE DE HOGAR";
          strOrigin = "A";
        }else if(strCodeField.equals("NUMERO_DOCUMENTO"))
        {
          strNomField = "No DOCUMENTO JEFE DE HOGAR";
          strOrigin = "N";
        }else
        {
          strNomField = "NUMERO DE BENEFICIOS";
          strOrigin = "N";
        }
        
        if(objAccion2.equals("Add"))
        {
          objCondConsVO = new CondicionConsultaVO();
          objCondConsVO.setCampo(strNomField);
          objCondConsVO.setCodcampo(strCodeField);
          objCondConsVO.setValor(strValue1);
          objCondConsVO.setValor2(strValue2);
          objCondConsVO.setOrigen(strOrigin);
          objCondConsVO.setCodigo(lFilters.size()+"");
          String strCodeFilter = request.getParameter("filter");
          int iFiltro = Integer.parseInt(strCodeFilter.trim());
          switch(iFiltro){ 
            case 1: 
                 objCondConsVO.setFiltro("[>=] Mayor o Igual");
                 objCondConsVO.setCodfiltro(">=");
            break;
            case 2: 
                 objCondConsVO.setFiltro("[<=] Menor o Igual");
                 objCondConsVO.setCodfiltro("<=");
            break;
            case 3: 
                 objCondConsVO.setFiltro("[=] Igual a");
                 objCondConsVO.setCodfiltro("=");
            break;
            case 4: 
                 objCondConsVO.setFiltro("[!=] Diferente a");
                 objCondConsVO.setCodfiltro("<>");
            break;
            case 5: 
                 objCondConsVO.setFiltro("[<] Menor");
                 objCondConsVO.setCodfiltro("<");
            break;
            case 6: 
                 objCondConsVO.setFiltro("[>] Mayor");
                 objCondConsVO.setCodfiltro(">");
            break;
            case 7: 
                 objCondConsVO.setFiltro("Entre");
                 objCondConsVO.setCodfiltro("between");
            break;
          }
          
          lFilters.add(objCondConsVO);
        }
      }
    }
    
    // Proceso para eliminar un filtro
    String strCodigo = request.getParameter("codigos");
    if(strCodigo != null)
    {
      CondicionConsultaVO objCondiCons = new CondicionConsultaVO();
      lFilters = (List)session.getAttribute("ListFiltrado");
      for(int s = 0;s<lFilters.size();s++)
      {
        objCondiCons = (CondicionConsultaVO)lFilters.get(s);
        if(objCondiCons.getCodigo().toString().equals(strCodigo))
        {
          lFilters.remove(s);
          
        }
      }
    }
    // Fin del proceso
    
    if(lFilters.size() != 0) 
    {
      session.setAttribute("ListFiltrado",lFilters);
    }
    
    request.setAttribute("CodeField",strCodeField);
    request.setAttribute("TypeData",strOrigin);
    request.setAttribute("ListShowFilters",lShowFilters);
    
    // Proceso para guardar los criterios de busqueda
    
    if(strCodeZona != null && !strCodeZona.equals("nulo"))
      session.setAttribute("CodeZona",strCodeZona);
    if(strCodeComuna != null && !strCodeComuna.equals("nulo"))
      session.setAttribute("CodeComuna",strCodeComuna);
    if(strCodeBarrio != null && !strCodeBarrio.equals("nulo"))
      session.setAttribute("CodeBarrio",strCodeBarrio);
    if(strCodePrograma != null && !strCodePrograma.equals("nulo"))
      session.setAttribute("CodePrograma",strCodePrograma);
    if(strCodeSubPrograma != null && !strCodeSubPrograma.equals("nulo"))
      session.setAttribute("CodeSubPrograma",strCodeSubPrograma);
    if(strDayIni != null && !strDayIni.equals("nulo"))
      session.setAttribute("DayIni",strDayIni);
    if(strMonthIni != null && !strMonthIni.equals("nulo"))
      session.setAttribute("MonthIni",strMonthIni);
    if(strYearIni != null && !strYearIni.equals("nulo"))
      session.setAttribute("YearIni",strYearIni);
    if(strDayFin != null && !strDayFin.equals("nulo"))
      session.setAttribute("DayFin",strDayFin);
    if(strMonthFin != null && !strMonthFin.equals("nulo"))
      session.setAttribute("MonthFin",strMonthFin);
    if(strYearFin != null && !strYearFin.equals("nulo"))
      session.setAttribute("YearFin",strYearFin);
    // Fin del proceso
    
    return mapping.findForward("success");
  }
}